package com.fate.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fate.constant.RoleConstants;
import com.fate.domain.SysRole;
import com.fate.mapper.SysRoleMapper;
import com.fate.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.Duration;
import java.util.Collection;
import java.util.List;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService{
    @Autowired
    private StringRedisTemplate redisTemplate;


    @Override
    public List<SysRole> list() {
        String roleStr = redisTemplate.opsForValue().get(RoleConstants.ROLE_PREFIX);
        List<SysRole> sysRolesList = null;
        if (StringUtils.hasText(roleStr)){
            //转换成list 返回
            sysRolesList = JSON.parseArray(roleStr, SysRole.class);
        }else {
            //查询 存放 返回
            sysRolesList = super.list();
            if (!CollectionUtils.isEmpty(sysRolesList)){
                redisTemplate.opsForValue().set(RoleConstants.ROLE_PREFIX,JSON.toJSONString(sysRolesList), Duration.ofDays(7));
            }
        }
        return sysRolesList;
    }

















}
